package javafx.com;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @Classname main
 * @Date 2021/09/19 09:56
 * @Author by yx
 * @Description :
 */
public class main extends Application {

    static boolean isManaged = false;
    static boolean isVisable = false;
    static double opacityValue = 0.0;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button3.setManaged(false)  ");
        Button button6 = new Button("button3.setVisible(false)");
        Button button7 = new Button("button3.setOpacity(0)");

//        button3.setManaged(false);
//        button3.setVisible(false);
//        button3.setOpacity(0);

        AnchorPane anchorPane = new AnchorPane();
        HBox hBox = new HBox();
        VBox vBox = new VBox();

        hBox.getChildren().addAll(button1, button2, button3, button4);
        hBox.setPadding(new Insets(20));
        hBox.setSpacing(10);
        vBox.getChildren().addAll(button5, button6, button7);
        vBox.setSpacing(10);

        anchorPane.setStyle("-fx-background-color: #006E7C");
        anchorPane.getChildren().addAll(hBox, vBox);
        AnchorPane.setTopAnchor(vBox, 100.0);
        AnchorPane.setLeftAnchor(vBox, 100.0);

        Scene scene = new Scene(anchorPane);

        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.setTitle("javafx");
        primaryStage.show();

        button5.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                button3.setManaged(isManaged);
                print(hBox);
                if (isManaged) {
                    isManaged = false;
                } else {
                    isManaged = true;
                }
                button5.setText("button3.setManaged(" + isManaged + ")");
            }
        });

        button6.setOnAction((event) -> {
            button3.setVisible(isVisable);
            print(hBox);
            if (isVisable) {
                isVisable = false;
            } else {
                isVisable = true;
            }
            button6.setText("button3.setVisible(" + isVisable + ")");
        });

        button7.setOnAction((event) -> {
            button3.setOpacity(opacityValue);
            print(hBox);
            if (opacityValue == 0.0) {
                opacityValue = 1.0;
            } else {
                opacityValue = 0.0;
            }
            button7.setText("button3.setOpacity(" + opacityValue + ")");
        });

    }

    public void print(HBox hBox) {
        System.out.println("hbox有" + hBox.getChildren().size() + "个组件");
    }
}
